﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class NewsUI
    {
        private const String CACHE_KEY = "News_GetAll";

        public int Add(int CategoryPageID, String MetaKeyword, String MetaDescription, String NewsTitle, String Alias, 
            String NewsAbstract, String NewsContent, String NewsImage, String NewsSource, String NewsVideo, int ViewTime,
            int ViewOrder, bool IsHot, bool IsTypical, bool IsNew, bool IsApproved, int ApprovedBy, DateTime CreateDate,
            int CreateBy, String UpdateProcess, String NewsProperties, String NewsType)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "News_add", CategoryPageID, MetaKeyword, MetaDescription, NewsTitle, Alias, 
                     NewsAbstract, NewsContent, NewsImage, NewsSource, NewsVideo, ViewTime,
                    ViewOrder,  IsHot, IsTypical, IsNew, IsApproved, ApprovedBy, CreateDate,
                    CreateBy,  UpdateProcess, NewsProperties, NewsType);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int NewsID, int CategoryPageID, String MetaKeyword, String MetaDescription, String NewsTitle, String Alias,
            String NewsAbstract, String NewsContent, String NewsImage, String NewsSource, String NewsVideo,
            int ViewOrder, bool IsHot, bool IsTypical, bool IsNew,
            String UpdateProcess, String NewsProperties, String NewsType)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "News_update", NewsID, CategoryPageID, MetaKeyword, MetaDescription, NewsTitle, Alias,
                     NewsAbstract, NewsContent, NewsImage, NewsSource, NewsVideo, 
                    ViewOrder, IsHot, IsTypical, IsNew,
                    UpdateProcess, NewsProperties, NewsType);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + NewsID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateView(int newsID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "News_UpdateView", newsID);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + newsID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetTop(int categoryId)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "New_GetTop", categoryId);
            }
            catch (Exception)
            {

                return null;
            }
        }
        public DataSet GetNew()
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "News_getNews");
            }
            catch (Exception)
            {

                return null;
            }

        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "News_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetApproved()
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "News_GetApproved");
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetLastest(int numberOfRow)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "News_GetLastest", numberOfRow);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetChildNewsByCategoryID(int categoryPageID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "News_GetChildNewsByCategoryID", categoryPageID);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByDate(DateTime date)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "News_GetByDate", date);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByCategogyPageID(int CategoryPageID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "News_GetByCategoryPageID",CategoryPageID);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID_SpecialForPhanTichKetQua(int NewsID)
        {
            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "News_GetByID_SpecialForPhanTichKetQua", NewsID);
                return ds;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int NewsID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + NewsID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "News_get", NewsID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + NewsID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int NewsID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "News_delete", NewsID);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        /// <summary>
        /// Cap nhat lai cac gia tri cho tin tuc
        /// </summary>
        /// <param name="ActionName">Ten Action, lay tu bien enum Utils.Common.ACTION_NAME</param>
        /// <param name="ProductListID">Danh sach cac NewsID, cach nhau bang dau phay</param>
        /// <returns>So ban ghi cap nhat thanh cong</returns>
        public int SetProperties(String ActionName, String NewsListID)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "News_Action", ActionName, NewsListID);
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
